package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysLoginLogBO;
import com.ztgf.sys.models.bo.SysLoginLogSaveBO;
import com.ztgf.sys.models.dto.SysLoginLogDTO;
import com.ztgf.sys.models.service.ISysLoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统用户登录日志表控制器
 *
 * @author YUE
 * @Date 2020-05-27 14:09:13
 */

@RestController
@RequestMapping("/cms/sysLoginLog")
@Slf4j
@Api(value = "SysLoginLogController",tags = "系统用户登录日志表接口")
public class SysLoginLogController {

    @DubboReference
    private ISysLoginLogService sysLoginLogService;

    @ApiOperation(value = "添加系统用户登录日志表",notes = "添加系统用户登录日志表")
    @PostMapping(value = "/insert")
    public Result insert(SysLoginLogSaveBO saveBO) {
        boolean flag = sysLoginLogService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysLoginLogDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysLoginLogDTO dto = sysLoginLogService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "系统用户登录日志表列表",notes = "系统用户登录日志表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysLoginLogDTO>> queryPageList(SysLoginLogBO bo){
        Wrapper<SysLoginLogDTO> wrapper = new EntityWrapper<>();
        if (StrUtils.isNotEmpty(bo.getStartDate())){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (StrUtils.isNotEmpty(bo.getEndDate())){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("create_time", false);
        Page<SysLoginLogDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysLoginLogService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
